package com.sky.controller.admin;

import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Setmeal;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.SetmealService;
import com.sky.vo.DishVO;
import com.sky.vo.SetmealVO;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/admin/setmeal")
public class SetmealController {
    @Autowired
    private SetmealService setmealService;
    /**
     * 新增套餐
     * @param setmealDTO
     */
    @ApiOperation("新增套餐功能")
    @PostMapping
    @CacheEvict(cacheNames ="setmealCache",key = "#setmealDTO.categoryId")
    public Result save(@RequestBody SetmealDTO setmealDTO){
        log.info("新增套餐,{}",setmealDTO);
        setmealService.save(setmealDTO);

        return Result.success();
    }
    /**
     * 根据id查询套餐
     * @param id
     */
    @ApiOperation("根据id查询套餐")
    @GetMapping("/{id}")
    public Result<SetmealVO> selectById(@PathVariable Long id){
        log.info("根据id查询套餐参数,{}",id);
        SetmealVO setmealVO=setmealService.selectByIdWithDishes(id);
        return Result.success(setmealVO);
    }
    /**
     * 套餐分页查询
     * @param setmealPageQueryDTO
     */
    @ApiOperation("套餐分页查询功能")
    @GetMapping("/page")
    public Result<PageResult> page(SetmealPageQueryDTO setmealPageQueryDTO){
        log.info("套餐分页查询参数,{}",setmealPageQueryDTO);
        PageResult pageResult=setmealService.pageQuery(setmealPageQueryDTO);
        return Result.success(pageResult);
    }
    /**
     * 套餐起售停售功能
     * @param status，id
     */
    @CacheEvict(cacheNames = "setmealCache",allEntries = true)
    @ApiOperation("套餐起售停售功能")
    @PostMapping("/status/{status}")
    public Result startOrStop(@PathVariable Integer status,Long id){
        log.info("套餐起售停售功能参数,{},{}",status,id);
        setmealService.startOrStop(status,id);
        return Result.success();
    }
    /**
     * 套餐批量删除
     * @param ids
     */
    @CacheEvict(cacheNames = "setmealCache",allEntries = true)
    @ApiOperation("套餐批量删除功能")
    @DeleteMapping
    public Result deleteByIds(@RequestParam List<Long> ids){
        log.info("套餐批量删除功能参数,{}",ids);
        setmealService.deleteByIds(ids);
        return Result.success();
    }
    /**
     * 套餐修改
     * @param setmealVO
     */
    @CacheEvict(cacheNames = "setmealCache",allEntries = true)
    @PutMapping
    @ApiOperation("套餐修改功能")
    public Result update(@RequestBody SetmealVO setmealVO){
        log.info("套餐修改功能参数,{}",setmealVO);
        setmealService.update(setmealVO);
        return Result.success();
    }



}
